package com.gxa.service.impl;

import com.gxa.common.base.LayuiResultJson;
import com.gxa.common.base.R;
import com.gxa.common.constant.Constants;
import com.gxa.domain.dto.SysCourseSaveDTO;
import com.gxa.domain.dto.SysCourseUpdateDTO;
import com.gxa.domain.entity.SysCourse;
import com.gxa.mapper.SysCourseMapper;
import com.gxa.service.SysCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SysCourseServiceImpl implements SysCourseService {


    @Autowired
    private SysCourseMapper sysCourseMapper;
    @Override
    public LayuiResultJson findSysCourseAll(Integer page, Integer limit) {
        LayuiResultJson layuiResultJson = new LayuiResultJson();
        Integer offset = (page - 1)*limit;
        Integer rows = limit;
        List<SysCourse> sysCourses = sysCourseMapper.findSysCourseList(offset,rows);
        layuiResultJson.setCode(Constants.OK);
        layuiResultJson.setMsg(Constants.MESSAGE_OK);
        Integer totalSize = sysCourseMapper.countSysCourse();
        System.out.println(totalSize);
        layuiResultJson.setCount(totalSize);
        layuiResultJson.setData(sysCourses);
        return layuiResultJson;
    }

    @Override
    public R addSysCourse(SysCourseSaveDTO sysCourseSaveDTO) {
        int result = sysCourseMapper.addSysCourse(sysCourseSaveDTO);
        if(result > 0 ){
            return R.success("添加成功！");
        }
        return R.error();
    }

    @Override
    public R updateSysCourse(SysCourseUpdateDTO sysCourseUpdateDTO) {
        int result = sysCourseMapper.updateSysCourse(sysCourseUpdateDTO);
        if(result > 0 ){
            return R.success("修改成功！");
        }
        return R.error();
    }

    @Override
    public R delSysCourseByCourseId(Integer courseId) {
        int result = sysCourseMapper.delSysCourseByCourseId(courseId);
        if(result > 0 ){
            return R.success("删除成功！");
        }
        return R.error();
    }

    @Override
    public LayuiResultJson search(String courseName, Integer page, Integer limit) {
        LayuiResultJson layuiResultJson = new LayuiResultJson();
        Integer offset = (page - 1)*limit;
        Integer rows = limit;
        List<SysCourse> sysCourses = sysCourseMapper.findSysCourseListByCondition(offset,rows,courseName);
        layuiResultJson.setCode(Constants.OK);
        layuiResultJson.setMsg(Constants.MESSAGE_OK);
        Integer totalSize = sysCourseMapper.countSysCourseByCondition(courseName);
        layuiResultJson.setCount(totalSize);
        layuiResultJson.setData(sysCourses);
        return layuiResultJson;
    }


}
